﻿/*---------------------------------------------------------------------------------------
 
	File Name:			editor.js	
	Create Time:		Jun 17th, 2007.
	Author:				Seamuz
	All Rights Reserved (c) Huohai Co.,Ltd
 
----------------------------------------------------------------------------------------*/

var doc = null, box = null, win = null, mode = 'design', created = false;
 
function createeditor()
{
	box	= form('EditForm').editbox;	 
	form('EditForm').submit= null;
	win = $('editwin');
	doc = editwin.document;
	
	doc.designMode = 'on';
	doc.contentEditable = true;
	doc.write('');
	doc.close();
	setTimeout('initcontent();', 100); 	  
	switchmode(mode);
	created = true;
}

function gettext() {
	return box.value;
}

function settext(text) {
	box.value = text;
}

function gethtml() {
	return doc.body.innerHTML;
}

function sethtml(html) {
	doc.body.innerHTML = html;
}

function switchmode(m, btn) {	
	switch(m) {
		case 'ubb':	{
			ubbmode();				
			break;
		}
		case 'design': {
			designmode();
			break;
		}
		case 'html': {
		htmlmode();
			break;
		}
	}
	
	mode = m;
	if (btn == null) return;
	form('EditForm').htmlbtn.className = 'ubb_unselected';
	form('EditForm').ubbbtn.className = 'ubb_unselected';
	form('EditForm').designbtn.className = 'ubb_unselected';
	btn.className = 'ubb_selected';
	focuseditor();
}
	
function ubbmode() {
	win.style.display = 'none';
	box.style.display = '';
	if (!created || mode == 'ubb') return;
	if (mode == 'html') {
		settext(html2ubb(gettext()));
	}
	else {
		settext(html2ubb(gethtml()));
	}
}
function htmlmode() {
	win.style.display = 'none';
	box.style.display = '';
	if (!created || mode == 'html') return;
	if (mode == 'ubb') {
		settext(ubb2html(gettext()));
	}
	else {
		settext(gethtml());
	}
}
function designmode() {
	
	win.style.display = '';
	box.style.display = 'none';	
	if (!created || mode == 'design') return;

	if (mode == 'html') {
		sethtml(gettext());
	}
	else {
		sethtml(ubb2html(gettext()));
	}
}
function fixdata() {
	if (mode == 'ubb')
	{
		sethtml(ubb2html(gettext()));
		settext(html2ubb(gethtml()));
	}
	if (mode == 'html')
		settext(html2ubb(gettext()));
	else
		settext(html2ubb(gethtml()));
}
function focuseditor() {
	if (mode == 'design') {
		doc.body.focus();
	}
	else {
		box.focus();
	}
}

function createtoolbar() {
	var buttons = ['bold', '粗体', 'italic', '斜体', 'underline', '下划线', 
				   '-', 
				   'fontname', '字体', 'fontsize', '大小', 'fontcolor', '字体颜色',
				   '-',
				   'justifyleft', '居左', 'justifycenter', '居中', 'justifyright', '居右', 
				   '-',
				   'insertorderedlist', '数字顺序', 'insertunorderedlist', '符号顺序', 'indent', '缩进', 'outdent', '取消缩进' ,'-','subscript', '上标', 'superscript', '下标', 
				   '',
				   'code', '插入程序源代码', 'hide', '插入回复可见内容','createlink', '超级链接', 'unlink', '删除超级链接', 'vote', '插入投票', 'mediaplay', '插入MediaPlay视频', 'flash', '插入Flash动画', 'real', '插入RealPlay视频','email', '插入Email地址', 'image',  '插入图片'
				  ];
	var bar = '<div>';
	
							
	for (var i = 0; i < buttons.length; i++) {
		if (buttons[i] == '') {
			bar += '<p class="partition"></p>';
			continue;
		}
		if (buttons[i] == '-') {
			bar += ' <img src="images/ubb/separator.gif" alt="" />';
			continue;
		}
		bar += ' <img src="images/ubb/' + buttons[i] + '.gif" name="' + buttons[i] + '" alt="' + buttons[i + 1] + '" class="ubb_button" />';
		i++
	} 
 
	bar += "</div><p class='partition'></p>";
	 
	var obj = $('toolbar');
	if (obj != null)
	{
	 	obj.innerHTML = bar;
	}
	
	var bts = document.getElementsByClassName('ubb_button');
	 
	
	for (var j =0; j < bts.length; j++)
	{
		if (bts[j].tagName.toLowerCase() != 'img') continue;
		if (ie)
			bts[j].onclick =  function()
			{
				cmd();
			}; 
		else
			bts[j].addEventListener("click", 
							 function(e) {
											cmd(e);
										  }, true);
	}
	 
}

function createemotion(start, count)
{
	var bar = '';
	for (var i = start; i < start + count; i++) {


		bar +=' <img src="emotions/' + i + '.gif" alt="" onclick="insertemotion(' + i + ')"/>';
	}
	var obj = $('emotion');
	if (obj != null)
	 	obj.innerHTML = bar;
}

function cmd(even)
{ 

	var srcEle = ie ? event.srcElement : even.target;
	var sCmd = srcEle.name;
	if (sCmd == 'vote')
	{
		showInputVoteDialog(even);
		return;
	}
	if (sCmd == 'email')
	{
		showInputEmailDialog(even);
		return;
	}
	if (sCmd == 'createlink')
	{
		showInputLinkDialog(even);
		return;
	}
	if (sCmd == 'image')
	{
		showInputImageDialog(even);
		return;
	}
	
	if (sCmd == 'fontsize')
	{
		showFontSizeDialog(even);
		return;
	}
	if (sCmd == 'fontname')
	{
		showFontNameDialog(even);
		return;
	}
	
	if (sCmd == 'mediaplay')
	{
		showInputMediaDialog(even);
		return;
	}
	
	if (sCmd == 'flash')
	{
		showInputFlashDialog(even);
		return;
	}
	
	if (sCmd == 'real')
	{
		showInputRealDialog(even);
		return;
	}
	
	if (sCmd == 'code')
	{
		showInputCodeDialog(even);
		return;
	}
	if (sCmd == 'hide')
	{
		showInputHideDialog(even);
		return;
	}
	if (sCmd == 'fontcolor')
	{
		showColorDialog(even);
		return;
	}
	execmd(sCmd, false, '');
}
function execmd(sCmd, bInterface, vValue) {
//focuseditor();	
	
	if (mode == 'design') {
		doc.execCommand(sCmd, (bInterface?bInterface : false), (vValue ? vValue : ''));
	}
	else 
	{
		var index = 1;
		if (mode == 'ubb') index = 0;
		
		var arr = {
			bold : [[ '[b]', '[/b]' ],[ '<b>', '</b>' ]],
			italic : [[ '[i]', '[/i]' ],[ '<i>', '</i>' ]],
			underline : [[ '[u]', '[/u]' ],[ '<u>', '</u>' ]],
			justifyleft : [[ '[align=left]', '[/align]' ],[ '<p align="left">', '</p>' ]],
			justifycenter : [[ '[align=center]', '[/align]' ],[ '<p align="center">', '</p>' ]],
			justifyright : [[ '[align=right]', '[/align]' ],[ '<p align="right">', '</p>' ]],
			insertorderedlist : [[ '[list=1][*]', '[/list]'],[ '<ol><li>', '</li></ol>']],
			insertunorderedlist : [[ '[list][*]', '[/list]' ],[ '<ul><li>', '</li></ul>' ]],
			indent : [[ '[indent]', '[/indent]' ], [ '<blockquote>', '</blockquote>' ]],
			subscript : [[ '[sub]', '[/sub]' ],[ '<sub>', '</sub>' ]],
			superscript : [[ '[sup]', '[/sup]' ],[ '<sup>', '</sup>' ]],
			fontname : [['[font=' + vValue + ']', '[/font]' ],['<font face="' + vValue + '">', '</font>' ]],
			forecolor : [['[color=' + vValue + ']', '[/color]' ],['<font color="' + vValue + '">', '</font>' ]],
			fontsize : [['[size=' + vValue + ']', '[/size]' ],['<font size="' + vValue + '">', '</font>' ]]
		};
		for (var a in arr) {
			if (a == sCmd) {
				applyubb(arr[a][index][0], arr[a][index][1]);
				break;
			}
		}				
	}
}


function inserthtml(shtml) { 
	focuseditor();
	if (ie)	{
		var range = doc.selection.createRange();
		if (mode == 'html')
			range.text = shtml;
		else
			range.pasteHTML(shtml);
	}
	else
	{
		sethtml(gethtml()+shtml)
	}
}

function insertubb(text) {
	focuseditor();
	if (document.selection && document.selection.createRange) {
		var range = document.selection.createRange();
		range.text = text;
	} else {
		box.value += text;
	}
}

function applyubb(prepend, append) {
	focuseditor();
	if (document.selection && document.selection.createRange) {
		var range = document.selection.createRange();
		range.text = prepend + range.text + append;					
	} else {
		box.value += text;
	}
}

function insert(str) {
	if (mode == 'design') {
		inserthtml(str);
	}
	else {
		insertubb(str);
	}
}
function insertemotion(num) {
	if (mode == 'ubb')	{
		insert('[em]' + num + '[/em]');
	}	
	else{
		insert('<img src="emotions/' + num + '.gif" alt="em" />');
	}
}

function checksubmit()
{
	if (checkpost())
		return true;
	else
	{
		
		$('post').disabled = false;
		return false;
	}
}

function checkpost()
{  
	var f = form('EditForm');
	$('post').disabled = true;
	if (f.Title != null)
	{
		if (f.Title.value == '')
		{
			 
			alert('请输入主题标题!');
			f.Title.focus();
			return false;
		}
		if (f.Title.length > 100)
		{
			 
			alert('主题标题过长!');
			f.Title.focus();
			
			return false;			
		}
	}
	
	var quickmode = (EditorMode == 'QuickNewTopic' || EditorMode == 'QuickNewReply');
	if (!quickmode) fixdata(); 
	
	if (f.editbox.value == '')
	{
		 
		alert('请输入发表内容！');
		if(!quickmode) focuseditor();
		else f.editbox.focus();		
		
		return false;
	} 
	
	var limitcontentsize = EditorMode.indexOf('Topic') > -1 ? TopicContentSize : ReplyContentSize;
	if (f.editbox.value.length > limitcontentsize)
	{
		 
		alert("内容过长，已经超出所允许的长度，您当前允许发表内容的长度为 " + limitcontentsize + " 个字符！");
		if(!quickmode) focuseditor();
		else f.editbox.focus();		
		
		return false;
	}
	
	if (!undefined(f.Require))
	{
		if (isNaN(f.Require.value))
		{
			alert('积分限制必须是数字！');
			f.Require.focus();
			
			return false;
		}
		if (parseInt(f.Require.value) > LimitRequire)
		{
			$('post').disabled = false;
			alert('您当前可以设置的积分限制不能超过' + LimitRequire);
			f.Require.focus();
			
			return false;
		}
	}
	
	if (!undefined(f.Price))
	{
		if (isNaN(f.Price.value))
		{
			 
			alert('主题售价必须是数字！');
			f.Price.focus();
			
			return false;
		}
		if (parseInt(f.Price.value) > LimitPrice)
		{
			 
			alert('您当前可以设置的主题售价不能超过' + LimitPrice);
			f.Price.focus();
			return false;
		}
	}
	return true;
}

function setframestyle() { 
	var head = doc.getElementsByTagName('HEAD').item(0);
	var style = doc.createElement('link');
	style.href = 'style.aspx?name=styles/editor.css';
	style.rel = 'stylesheet';
	style.type = 'text/css';
	head.appendChild(style);
}

function initcontent() {
	sethtml(ubb2html(gettext()));
	stat();
	setframestyle();
	if (ie)
	{
		box.onkeydown = function()
		{
			
			return ctlent(event);
		}
		box.onfocus = function()
		{
			 hideDialog();
		}
		doc.body.onfocus = function()
		{
			 hideDialog();
		}
		 
		doc.body.onkeydown = function(){
			if(editwin.window.event.ctrlKey && editwin.window.event.keyCode == 13)
			{ 
				submitonce();
				return false;
			}
		}
	}
	else
	{		
		box.addEventListener("keydown", 
							 function(e) {
											if(e.ctrlKey && e.keyCode == 13)
											{
												submitonce();
											} 
										  }, true);
		box.addEventListener("focus", 
							 function(e) { 
							  hideDialog();
										  }, true);
		
		doc.addEventListener("focus", 
							 function(e) { 
							  hideDialog();
										  }, true);
		 
		doc.addEventListener("keydown", function(e)
										{
										  if(e.ctrlKey && e.keyCode == 13)
										  {
												submitonce();
												return false;
										  }
										}, true);
	}
	
	createtoolbar();
	createemotion(1, 20);
}

//Ctrl + Enter
function ctlent(e)
{
	 if(e.ctrlKey && e.keyCode == 13)
		{
			submitonce();
			return false;
		} 
}
function submitonce()
{
	$('post').click();
}
 
var filecount = 0;
var temp = '<div style="margin-bottom:3px;"><input type="file" name="uploader{id}" id="uploader{id}" size="60" onchange="uploadfile(this);" contentEditable="false" />';
function onuploadfile()
{
	if (LeftCount < 1)
	{
		alert("您今天不能再上传文件了");
		return;
	}
	filecount ++;
	var ctrl = temp.replace('{id}', filecount).replace('{id}', filecount);	
	var div = document.createElement("div");
	div.setAttribute("id", "dup" + filecount);
	div.innerHTML = ctrl + '&nbsp;&nbsp;&nbsp;售价：<input type="text" value="0" style="height:19px; line-height:19px;" name="downloadrequire' + filecount + '" size="5"/>&nbsp;金钱&nbsp;&nbsp;&rsaquo;&rsaquo;<a href="javascript:clearfile(' + filecount + ');">&nbsp;删除&nbsp;</a><span class="t1" id="size' + filecount + '"></span></div>';
	$('filecontainer').appendChild(div); 	 
	 
}
function endWith(s1,s2)  
  {   
      if(s1.length<s2.length)   
        return   false;   
      if(s1==s2)   
        return   true;   
      if(s1.substring(s1.length-s2.length)==s2)   
          return   true;   
  }
function checkuploadfile(file)
{
	var types = AttachType.split('|');
	var systypes = SystemAttachType.split('|');
	var vext = false;
	if (!IsAdmin)
	{
		for(var i = 0 ; i < types.length; i++)
		{
			if (endWith(file.value.toLowerCase(), types[i].toLowerCase()))
			{
				vext = true;
				break;
			}
		}
	}
	for(var i = 0 ; i < systypes.length; i++)
	{
		if (endWith(file.value.toLowerCase(), systypes[i].toLowerCase()))
		{
			vext = true;
			break;
		}
	}
	
	if (!vext)
	{
		alert("系统允许上传的文件类型为" + SystemAttachType + "，您所在用户允许上文件类型为 " + AttachType + "，您能上传的文件类型必须是系统允许类型的子集。");
		return false;
	}
	
	var filesize = getFileSize(file);
	if (filesize != -1)
	{
		if (filesize > AllowAttachSize)
		{
			alert("文件过大，最大只允许上传" + size(AllowAttachSize) + "的文件");
			return false;
		}
		if (filesize > LeftSize)
		{
			alert("文件过大，您今天只能上传" + size(LeftSize) + "了");
			return false;
		}
		if (LeftCount < 1)
		{
			alert("您今天不能再上传文件了");
			return false;
		}
		if (ie) $('size' + file.name.replace('uploader', '')).innerHTML = '(' + size(filesize) + ')';
		LeftSize -= filesize;
	}
	LeftCount --;
	return true;
}
function uploadfile(file)
{	
	if (!checkuploadfile(file))	
	{
		clearfile(file.name.replace('uploader', ''), true);
		removeuploadfile(file.name.replace('uploader', ''));
		return;
	}
	var old = file.name;
	fixdata();
	if (gettext().indexOf('[uploadimage]' + old.replace('uploader', '') + '[/uploadimage]') > -1 ||
		gettext().indexOf('[uploadfile]' + old.replace('uploader', '') + '[/uploadfile]') > -1)	
	{
		file.name = 'temp';
		file.id = 'temp';
		removeuploadfile(old.replace('uploader', ''));
		file.id = file.name = old;
	}
	else
	{
		focuseditor();
		var type = '';
		var t = getAttachType(file.value)
		if (t == 1)
		{  
			type = 'image';
		}
		else
		{ 
			type = 'file';
		}
		if (mode == 'ubb')	{
			insert('[upload'  + type + ']' + file.name.replace('uploader', '') + '[/upload' + type + ']\r\n');
		}	
		else{
			if (t != 1)
				insert('<br /><img src="images/icon/' + getIcon(t) + '" alt="uploadfile' + file.name.replace('uploader', '') + ',' + getFileName(file.value) + 'ATTACHNAMEENDFLAG" /><abbr>' + getFileName(file.value) + '</abbr><br />');
			else
				insert('<br /><img src="' + file.value + '" alt="uploadimage' + file.name.replace('uploader', '') + ',' + getFileName(file.value) + 'ATTACHNAMEENDFLAG" /><br />');
		}
	}
}

function removeuploadfile(index)
{
	if (mode == 'design')	{
		sethtml(ubb2html(html2ubb(gethtml())));
 	}
	if (mode == 'html')	{
		settext(ubb2html(html2ubb(gettext())));
 	}
}

function clearfile(id, flag)
{
	var elem = $('dup' + id);
	elem.parentNode.removeChild(elem);
	removeuploadfile(id);
	if (undefined(flag))
	{
		var uploader = $('uploader' + id);
		LeftSize -= getFileSize(uploader);
		LeftCount++;
	}
}
 

function changetopictype(t)
{
	 
}
 
var votecount = 0;
function insertVote()
{
	if ($('voteitems').value == '') 
	{
		alert('请输入投票项！');
		$('voteitems').focus();
		return;
	}
	if(trim($('voteitems').value).split('\n').length < 2)
	{
		alert('投票项必须2项或以上！');
		$('voteitems').focus();
		return;
	}
	if ($('VoteExpirty').value == '')
	{
		alert('请输入有效天数！');
		$('VoteExpirty').focus();
		return;
	}
	
	if (isNaN($('VoteExpirty').value))
	{
		alert('有效天数必须是数字！');
		$('VoteExpirty').focus();
		return;
	}
	
	if ($('VoteRequire').value == '')
	{
		alert('请输入投票限制！');
		$('VoteRequire').focus();
		return;
	}
	
	if (isNaN($('VoteRequire').value))
	{
		alert('投票限制必须是数字！');
		$('VoteExpirty').focus();
		return;
	}
	
	votecount ++;
	var div = document.createElement("div");
	div.setAttribute("id", "dvote" + votecount);
	div.innerHTML ='<textarea name="voteitems' + votecount + '">' + $('voteitems').value + '</textarea><input name="VoteExpirty' + votecount + '"  value="' + $('VoteExpirty').value + '" /></textarea><input name="VoteRequire' + votecount + '"  value="' + $('VoteRequire').value + '" />';
	if ($('multivote').checked)
		div.innerHTML += '<input name="multivote' + votecount + '"  value="1" />';
	$('votecontainer').appendChild(div);
	
	insert('[vote]' + votecount + '[/vote]');
	hideDialog();	
}

function showDialog(even, w, h, html)
{
	var src = document.all ? event.srcElement : even.target;	
	var x = src.offsetLeft, y = src.offsetTop; 
	var e = src.offsetParent;
	x += e.offsetLeft; 
	y += e.offsetTop;
	while(e=e.offsetParent) 
	{ 
		x += e.offsetLeft; 
		y += e.offsetTop;
	}
	
	var div = $('inputdialog');
	if (div == null)
	{
		div = document.createElement("div");
		div.id = 'inputdialog';
		div.className = 'inputdialog';
		div.style.position = 'absolute';
		div.style.padding = '5px';
		document.body.appendChild(div);
	}
	div.style.top = y + src.offsetHeight + 'px';
	div.style.left = x + 'px';
	div.style.width = w + 'px';
	div.style.height = h + 'px';
	div.innerHTML = html;
}
function showInputLinkDialog(even)
{  
	showDialog(even, 280, 75, '输入超级链接<p class="partition"></p><input type="text" id="inputlink" size="50" value="http://" /><p class="partition"></p><input type="button" class="button" value=" 确 定 " onclick="insertLink()" /> <input type="button" class="button" value=" 取 消 " onclick="hideDialog()" />');
	
}
function showInputEmailDialog(even)
{  
	showDialog(even, 280, 75, '输入Email地址<p class="partition"></p><input type="text" id="inputemail" size="50" value="" /><p class="partition"></p><input type="button" class="button" value=" 确 定 " onclick="insertEmail()" /> <input type="button" class="button" value=" 取 消 " onclick="hideDialog()" />');
	
}
function showInputImageDialog(even)
{  
	showDialog(even, 280, 75, '输入图像地址<p class="partition"></p><input type="text" id="inputimage" size="50" value="http://" /><p class="partition"></p><input type="button" class="button" value=" 确 定 " onclick="insertImage()" /> <input type="button" class="button" value=" 取 消 " onclick="hideDialog()" />');
	
}
function showInputCodeDialog(even)
{  
	showDialog(even, 500, 210, '输入程序源代码<p class="partition"></p><textarea  id="inputcode" style="width:98%; height:150px;"></textarea><p class="partition"></p><input type="button" class="button" value=" 确 定 " onclick="insertCode()" /> <input type="button" class="button" value=" 取 消 " onclick="hideDialog()" />');
	
}
function showInputHideDialog(even)
{  
	showDialog(even, 500, 210, '输入回复可见内容<p class="partition"></p><textarea  id="inputhide" style="width:98%; height:150px;"></textarea><p class="partition"></p><input type="button" class="button" value=" 确 定 " onclick="insertHide()" /> <input type="button" class="button" value=" 取 消 " onclick="hideDialog()" />');
	
}

function showInputFlashDialog(even)
{ 	
	showDialog(even, 320, 100, '插入 Flash 对象<p class="partition"></p>宽&nbsp;&nbsp;：<input type="text" id="flashwidth" value="600" size="5" /> px&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;高&nbsp;&nbsp;：<input type="text" id="flashheight" value="400" size="5"/> px<p class="partition"></p>URL：<input type="text" id="flashurl" value="http://" size="50" /><p class="partition"></p><input type="button" class="button" value=" 确 定 " onclick="insertFlash()" /> <input type="button" class="button" value=" 取 消 " onclick="hideDialog()" />');	 
}

function showInputRealDialog(even)
{ 	
	showDialog(even, 320, 100, '插入 RealPlayer 视频<p class="partition"></p>宽&nbsp;&nbsp;：<input type="text" id="realwidth" value="600" size="5" /> px&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;高&nbsp;&nbsp;：<input type="text" id="realheight" value="400" size="5"/> px<p class="partition"></p>URL：<input type="text" id="realurl" value="http://" size="50" /><p class="partition"></p><input type="button" class="button" value=" 确 定 " onclick="insertReal()" /> <input type="button" class="button" value=" 取 消 " onclick="hideDialog()" />');	 
}
function showInputMediaDialog(even)
{ 	
	showDialog(even, 320, 100, '插入 MediaPlayer 视频<p class="partition"></p>宽&nbsp;&nbsp;：<input type="text" id="mediawidth" value="600" size="5" /> px&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;高&nbsp;&nbsp;：<input type="text" id="mediaheight" value="400" size="5"/> px<p class="partition"></p>URL：<input type="text" id="mediaurl" value="http://" size="50" /><p class="partition"></p><input type="button" class="button" value=" 确 定 " onclick="insertMedia()" /> <input type="button" class="button" value=" 取 消 " onclick="hideDialog()" />');	 
}
function showInputVoteDialog(even)
{ 	
	showDialog(even, 450, 190, '投票项目&nbsp;<span style="color:#F00">*</span><p class="partition"></p><p class="an">有效天数&nbsp;<input name="VoteExpirty" id="VoteExpirty" class="input2" onBlur="this.className=\'input2\'" onFocus="this.className=\'input1\'" size="5" value="0" />&nbsp;(小于1天表示永不过期)&nbsp;&nbsp;&brvbar;&nbsp;&nbsp;至少有多少积分才可投票：<input name="VoteRequire" id="VoteRequire" class="input2" onBlur="this.className=\'input2\'" onFocus="this.className=\'input1\'" size="5" value="0" /></p><p class="partition"></p><textarea name="voteitems" id="voteitems" rows="10" style="height:100px;width:98%; padding:2px;"></textarea><p class="partition"></p><p class="an"><input id="multivote" type="checkbox" name="multivote" class="smallbox" value="1">&nbsp;<span class="t1">允许多选</span>&nbsp;&nbsp;&nbsp;<input type="button" class="button" value="&nbsp;&nbsp;&nbsp;确定&nbsp;&nbsp;&nbsp;" onClick="insertVote();"/>&nbsp;&nbsp;&nbsp;&nbsp;<input type="button"  class="button" value="&nbsp;&nbsp;&nbsp;取消&nbsp;&nbsp;&nbsp;" onClick="hideDialog();"/></p>');	 
}


function showColorDialog(even)
{
	var colorlist=new Array(40);
	var ocbody = '';
	colorlist[0]="#000000"; colorlist[1]="#993300"; colorlist[2]="#333300"; colorlist[3]="#003300"; 
	colorlist[4]="#003366"; colorlist[5]="#000080"; colorlist[6]="#333399"; colorlist[7]="#333333"; 
	
	colorlist[8]="#800000"; colorlist[9]="#FF6600"; colorlist[10]="#808000";colorlist[11]="#008000"; 
	colorlist[12]="#008080";colorlist[13]="#0000FF";colorlist[14]="#666699";colorlist[15]="#808080"; 
	
	colorlist[16]="#FF0000";colorlist[17]="#FF9900";colorlist[18]="#99CC00";colorlist[19]="#339966"; 
	colorlist[20]="#33CCCC";colorlist[21]="#3366FF";colorlist[22]="#800080";colorlist[23]="#999999"; 
	
	colorlist[24]="#FF00FF";colorlist[25]="#FFCC00";colorlist[26]="#FFFF00";colorlist[27]="#00FF00"; 
	colorlist[28]="#00FFFF";colorlist[29]="#00CCFF";colorlist[30]="#993366";colorlist[31]="#CCCCCC"; 
	
	colorlist[32]="#FF99CC";colorlist[33]="#FFCC99";colorlist[34]="#FFFF99";colorlist[35]="#CCFFCC"; 
	colorlist[36]="#CCFFFF";colorlist[37]="#99CCFF";colorlist[38]="#CC99FF";colorlist[39]="#FFFFFF"; 
	
	ocbody = ""; 
ocbody += "<table cellspacing='2' cellpadding='1'>"; 
	for(var i=0;i<colorlist.length;i++) { 
		if(i%8==0) 
			ocbody += "<tr>"; 
		ocbody += "<td width=\"12\" height=\"12\" style=\"border:1px solid #FFFFFF;\" onMouseOut=\"colordialogmouseout(this);\" onMouseOver=\"colordialogmouseover(this);\" onMouseDown=\"colordialogmousedown('"+colorlist[i]+"')\" align=\"center\" valign=\"middle\"><table style=\"border:1px solid #808080;\" width=\"12\" height=\"12\" bgcolor=\""+colorlist[i]+"\"><tr><td></td></tr></table></td>"; 
		if(i%8==7) 
			ocbody += "</tr>"; 
	} 
ocbody += "</table>"; 
showDialog(even, 147, 93, ocbody);
}

function showFontSizeDialog(even)
{
	ocbody = ""; 
ocbody += "<table cellspacing='0' cellpadding='3' width='100%'>"; 
	for(var i=1;i<8;i++) {  
			ocbody += "<tr>"; 
		ocbody += "<td onMouseOut=\"colordialogmouseout(this);\" onMouseOver=\"colordialogmouseover(this);\" onMouseDown=\"sizedialogmousedown('"+i+"')\" align=\"center\" valign=\"middle\"><font size='" + i + "'>" + i + "</font></td></tr>"; 
	} 
ocbody += "</table>"; 
showDialog(even, 37, 240, ocbody);
}

function showFontNameDialog(even)
{
	var faces = ['宋体', '黑体','sans-serif','courier new','arial','tahoma','verdana','FixedSys'];
var ocbody = ""; 
ocbody += "<table cellspacing='0' cellpadding='3' width='100%'>"; 

for(var i=0; i < faces.length; i++)
ocbody += "<tr><td onMouseOut=\"colordialogmouseout(this);\" onMouseOver=\"colordialogmouseover(this);\" onMouseDown=\"fontdialogmousedown('"+faces[i]+"')\" align=\"left\" valign=\"middle\"><font face='" + faces[i] + "'>" + faces[i] + "</font></td></tr>"; 
 
ocbody += "</table>"; 
showDialog(even, 100, 180, ocbody);
}


function colordialogmouseout(obj){ 
obj.style.borderColor="#FFFFFF"; 
obj.bgColor="";
}

function colordialogmouseover(obj){ 
obj.style.borderColor="#333333"; 
obj.bgColor="#FFFFFF"; 
} 
function colordialogmousedown(color){
execmd('forecolor', false, color);
hideDialog();
focuseditor();
} 
function sizedialogmousedown(s){
execmd('fontsize', false, s);
hideDialog();
focuseditor();
} 
function fontdialogmousedown(s){
execmd('fontname', false, s);
hideDialog();
focuseditor();
} 
function insertLink()
{
	var m = $('inputlink').value;
	
	if (mode == 'ubb')	{
		insert('[url=' + m + ']' + m + '[/url]');
	}	
	else{
		insert('<a href="' + m + '">' + m + '</a>');
	}
	hideDialog();
}
function insertEmail()
{
	var m = $('inputemail').value;
	if (m.search(/^[^\s@:"'<>,&]{2,}@[^\[\.]+\.[^\[]{2,}$/gi) < 0)
   	{
		$('inputemail').focus();
	   alert('Email 地址格式不正确');
	   return;
	   
   	}
	if (mode == 'ubb')	{
		insert('[email=' + m + ']' + m + '[/email]');
	}	
	else{
		insert('<a href="mailto:' + m + '">' + m + '</a>');
	}
	hideDialog();
}

function insertImage()
{
	var m = $('inputimage').value;
	 
	if (mode == 'ubb')	{
		insert('[img]' + m + '[/img]');
	}	
	else{
		insert('<img src="' + m + '" />');
	}
	hideDialog();
}
function insertCode()
{
	if (mode == 'ubb')	{
		insert('[code]\n' + $('inputcode').value + '\n[/code]');
	}	
	else{
		insert('[code]\n' + ubb2html($('inputcode').value) + '\n[/code]');
	}
	hideDialog();
}
function insertHide()
{
	insert('[hide]\n' + ubb2html($('inputhide').value) + '\n[/hide]');
	hideDialog();
}
function insertFlash()
{ 
	var w = parseInt($('flashwidth').value);
	var h = parseInt($('flashheight').value);
	
	if(w > 1024)
		w = 600;
	if (h > 768)
		h = 400;
	insert('[flash=' + w + ',' + h + ']' + $('flashurl').value + '[/flash]'); 
	hideDialog();
}

function insertReal()
{ 
	var w = parseInt($('realwidth').value);
	var h = parseInt($('realheight').value);
	
	if(w > 1024)
		w = 600;
	if (h > 768)
		h = 400;
	insert('[rm=' + w + ',' + h + ']' + $('realurl').value + '[/rm]'); 
	hideDialog();
}
function insertMedia()
{ 
	var w = parseInt($('mediawidth').value);
	var h = parseInt($('mediaheight').value);
	
	if(w > 1024)
		w = 600;
	if (h > 768)
		h = 400;
	insert('[mp=' + w + ',' + h + ']' + $('mediaurl').value + '[/mp]'); 
	hideDialog();
}


function hideDialog()
{
	var div = $('inputdialog');
	if (div != null)
		div.parentNode.removeChild(div);
}
function resize(span)
{
	var bh = parseInt(box.style.height);
	var wh = parseInt(win.style.height);
	if (span < 0 && bh <= 100) return;
	if (span > 0 && bh >= 800) return;
	box.style.height = span + bh + 'px'
	win.style.height = span + wh + 'px'
}

function getCurrentContent()
{
	if (mode == 'ubb')
		return gettext();
	if (mode == 'html')
		return html2ubb(gettext());
	return html2ubb(gethtml());
}
function setCurrentContent(content)
{
	if (mode == 'ubb')
		settext(content);
	else
		sethtml(ubb2html(content)); 
}

function stat()
{
	var totals = 0;
	if (EditorMode.indexOf('Topic') > -1) 
		totals = TopicContentSize;
	else
		totals = ReplyContentSize;
	var content = getCurrentContent();
	var l = totals - content.length;
	if (l < 0) 
	{
		alert("已经达到限制字符数，系统将自动截断输入内容");
		setCurrentContent(content.substring(0, totals));
	}
	
	$('leftcontentsize').innerHTML = l;
	setTimeout(stat, 500);
}